﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Dycox.Reporting
{
    internal class PhysicalCell
    {
        internal PhysicalCell(int rowIndex, int columnIndex)
        {
            _rowIndex = rowIndex;
            _columnIndex = columnIndex;
        }

        private TableCell _cell;

        public TableCell Cell
        {
            get { return _cell; }
        }

        private int _columnIndex;

        public int ColumnIndex
        {
            get { return _columnIndex; }
        }

        private int _rowIndex;

        public int RowIndex
        {
            get { return _rowIndex; }
        }

        private CellOccupation _occupation;

        public CellOccupation Occupation
        {
            get { return _occupation; }
        }

        public void SetCell(TableCell cell, CellOccupation occupation)
        {
            if (cell == null)
                throw new ArgumentNullException("cell");

            Clear();

            //if (cell.OccupiedCells == null)
            //    cell.OccupiedCells = new List<PhysicalCell>();
            
            //System.Diagnostics.Debug.Assert(!cell.OccupiedCells.Contains(this), "Why is the cell already occupied?");
            //if (occupation == CellOccupation.Normal)
            //    System.Diagnostics.Debug.Assert(cell.OccupiedCells.Count == 0, "Cell should not occupy any physical cell.");

            //cell.OccupiedCells.Add(this);
            _cell = cell;
            _occupation = occupation;

            if (occupation == CellOccupation.Normal)
                cell.PhysicalCell = this;
        }

        internal void Clear()
        {
            //if (_cell != null)
            //{
            //    System.Diagnostics.Debug.Assert(_cell.OccupiedCells != null);
            //    _cell.OccupiedCells.Remove(this);
            //}

            if (_occupation == CellOccupation.Normal && _cell != null)
            {
                System.Diagnostics.Debug.Assert(_cell.PhysicalCell == this);
                _cell.PhysicalCell = null;
            }

            _cell = null;
            _occupation = CellOccupation.Normal;
        }
    }

    internal enum CellOccupation
    {
        Normal = 0,
        SpanLeft = 1,
        SpanAbove = 2,
        SpanBoth = 3
    }
}
